package xin.vote.controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import xin.vote.model.Players;
import xin.vote.service.IPlayerService;
import xin.vote.util.Result;

@Controller
@RequestMapping("/player")
public class PlayerController {
	@Resource
	private IPlayerService playerService;

	@RequestMapping("/findplayerdynamic")
	@ResponseBody
	public Result findAllPlayer(@RequestParam(value = "playerNo", required = false) String playerNo,
			@RequestParam(value = "playerId", required = false) Integer playerId) {// defaultValue="0",这里是null
		Players p = new Players();
		p.setPlayerno(playerNo);
		p.setPlayerid(playerId);
		List<Players> playerList = playerService.findAllPlayer(p);
		// 时间转换
		String jsonarry = JSONArray.toJSONStringWithDateFormat(playerList, "yyyy-MM-dd HH:mm");
		Result result = new Result();
		result.setCode(0);
		result.setMsg("成功");
		result.setLimit(10);
		result.setCount(playerList.size());
		result.setData(playerList);
		return result;
	}

	// 准备数据，下拉框
	@RequestMapping("/preplayer")
	@ResponseBody
	public List<Players> findEmployee() {
		List<Players> list = playerService.findAllPlayer();
		return list;
	}

	// 新增
	@RequestMapping("/addplayer")
	@ResponseBody
	public String updateSale(@RequestParam(value = "playerNo") String playerNo,
			@RequestParam(value = "playerName") String playerName,
			@RequestParam(value = "playerImage", required = false) String playerImage) {
		Players p = new Players();
		p.setPlayerno(playerNo);
		p.setPlayername(playerName);
		p.setPlayerimage(playerImage);
		int ret = playerService.addPlayer(p);
		if (ret > 0) {
			return "1";
		} else {
			return "0";
		}
	}

	// 修改
	@RequestMapping("/updateplayer")
	@ResponseBody
	public String updatePlayer(@RequestParam(value = "playerId") Integer playerId,
			@RequestParam(value = "playerNo", required = false) String playerNo,
			@RequestParam(value = "playerName", required = false) String playerName,
			@RequestParam(value = "playerImage", required = false) String playerImage) {
		System.out.println("000"+playerImage);
		Players p = new Players();
		p.setPlayerid(playerId);
		p.setPlayerno(playerNo);
		p.setPlayername(playerName);
		p.setPlayerimage(playerImage);
		int ret = playerService.updatePlayer(p);
		if (ret > 0) {
			return "1";
		} else {
			return "0";
		}
	}

	// delete player

	@RequestMapping("/deleteplayer")
	@ResponseBody
	public String deletePlayer(@RequestParam(value = "playerId", required = false) Integer playerId) {
		int ret = playerService.deletePlayer(playerId);
		if (ret > 0) {
			return "1";
		} else {
			return "0";
		}
	}

	@RequestMapping("/uploadHeadImage")
	@ResponseBody
	public JSONObject uploadHeadImage(@RequestParam("file") MultipartFile file, @ModelAttribute Players player,
			HttpServletRequest request, InputStream stream) throws IOException {
		Assert.notNull(file, "上传文件不能为空");
		String src = "images/";
		// String path = request.getSession().getServletContext().getRealPath("/"+src);
//		String path = "D:/java/workspace/vote/src/main/webapp/images";
		String path = "D:/temp/images";
		JSONObject json = new JSONObject();
		// System.currentTimeMillis()根据系统时间产生随机数，保证上传的图片名字不一样
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式
		String date = sdf.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
//		String name=System.currentTimeMillis()+file.getOriginalFilename();
		synchronized (this) {// 锁上picname
			String picname = date + file.getOriginalFilename();
			File dir = new File(path, picname);
			src = src + picname;
			if (!dir.exists()) {
				dir.mkdirs();
				json.put("msg", "上传成功");
				json.put("code", 0);
				json.put("src", src);
			} else {
				json.put("msg", "上传失败");
				json.put("code", 1);
			}
			file.transferTo(dir);
			return json;
		}
	}
}
